/*
 *  Copyright 2011 yuxinlei.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package camel.first.api.datastore.internal.dao.impl;

import camel.first.api.datastore.dao.CoverageEntityJpaController;
import camel.first.api.datastore.dao.exceptions.IllegalOrphanException;
import camel.first.api.datastore.dao.exceptions.NonexistentEntityException;
import camel.first.api.datastore.dao.exceptions.PreexistingEntityException;
import camel.first.api.datastore.internal.entity.CoverageEntity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityNotFoundException;

import camel.first.api.datastore.internal.entity.BuildsEntity;
import java.util.ArrayList;

/**
 *
 * @author yuxinlei
 */
public class CoverageEntityJpaControllerImpl implements CoverageEntityJpaController {

    public CoverageEntityJpaControllerImpl() {
        emf = Persistence.createEntityManagerFactory("reportdb");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CoverageEntity coverageEntity) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        BuildsEntity buildsByBuildIdOrphanCheck = coverageEntity.getBuildsByBuildId();
        if (buildsByBuildIdOrphanCheck != null) {
            CoverageEntity oldCoverageByIdOfBuildsByBuildId = buildsByBuildIdOrphanCheck.getCoverageById();
            if (oldCoverageByIdOfBuildsByBuildId != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The BuildsEntity " + buildsByBuildIdOrphanCheck + " already has an item of type CoverageEntity whose buildsByBuildId column cannot be null. Please make another selection for the buildsByBuildId field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            BuildsEntity buildsByBuildId = coverageEntity.getBuildsByBuildId();
            if (buildsByBuildId != null) {
                buildsByBuildId = em.getReference(buildsByBuildId.getClass(), buildsByBuildId.getId());
                coverageEntity.setBuildsByBuildId(buildsByBuildId);
            }
            em.persist(coverageEntity);
            if (buildsByBuildId != null) {
                buildsByBuildId.setCoverageById(coverageEntity);
                buildsByBuildId = em.merge(buildsByBuildId);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCoverageEntity(coverageEntity.getBuildId()) != null) {
                throw new PreexistingEntityException("CoverageEntity " + coverageEntity + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CoverageEntity coverageEntity) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CoverageEntity persistentCoverageEntity = em.find(CoverageEntity.class, coverageEntity.getBuildId());
            BuildsEntity buildsByBuildIdOld = persistentCoverageEntity.getBuildsByBuildId();
            BuildsEntity buildsByBuildIdNew = coverageEntity.getBuildsByBuildId();
            List<String> illegalOrphanMessages = null;
            if (buildsByBuildIdNew != null && !buildsByBuildIdNew.equals(buildsByBuildIdOld)) {
                CoverageEntity oldCoverageByIdOfBuildsByBuildId = buildsByBuildIdNew.getCoverageById();
                if (oldCoverageByIdOfBuildsByBuildId != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The BuildsEntity " + buildsByBuildIdNew + " already has an item of type CoverageEntity whose buildsByBuildId column cannot be null. Please make another selection for the buildsByBuildId field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (buildsByBuildIdNew != null) {
                buildsByBuildIdNew = em.getReference(buildsByBuildIdNew.getClass(), buildsByBuildIdNew.getId());
                coverageEntity.setBuildsByBuildId(buildsByBuildIdNew);
            }
            coverageEntity = em.merge(coverageEntity);
            if (buildsByBuildIdOld != null && !buildsByBuildIdOld.equals(buildsByBuildIdNew)) {
                buildsByBuildIdOld.setCoverageById(null);
                buildsByBuildIdOld = em.merge(buildsByBuildIdOld);
            }
            if (buildsByBuildIdNew != null && !buildsByBuildIdNew.equals(buildsByBuildIdOld)) {
                buildsByBuildIdNew.setCoverageById(coverageEntity);
                buildsByBuildIdNew = em.merge(buildsByBuildIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = coverageEntity.getBuildId();
                if (findCoverageEntity(id) == null) {
                    throw new NonexistentEntityException("The coverageEntity with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CoverageEntity coverageEntity;
            try {
                coverageEntity = em.getReference(CoverageEntity.class, id);
                coverageEntity.getBuildId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The coverageEntity with id " + id + " no longer exists.", enfe);
            }
            BuildsEntity buildsByBuildId = coverageEntity.getBuildsByBuildId();
            if (buildsByBuildId != null) {
                buildsByBuildId.setCoverageById(null);
                buildsByBuildId = em.merge(buildsByBuildId);
            }
            em.remove(coverageEntity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }



    public CoverageEntity findCoverageEntity(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CoverageEntity.class, id);
        } finally {
            em.close();
        }
    }


}
